<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

class Caidan_mdl extends MY_Model {

	const DB_CAIDAN = 'caidan';
	const DB_CAIDAN_CAIPU = 'caidan_caipu';
	const DB_CAIPU = 'caipu';

    public function __construct()
    {
		parent::__construct();
		$this->table = 'caidan';
    }
    //菜单下菜谱***new
    public function get_caipu_by_caidan($caidan_id, $offset = 0, $limit = 5) {
    	$this->db->select("".self::DB_CAIPU.".id, ".self::DB_CAIPU.".title, ".self::DB_CAIPU.".coverImage");
		$this->db->join(self::DB_CAIDAN_CAIPU, "".self::DB_CAIDAN_CAIPU.".caipu_id = ".self::DB_CAIPU.".id");

		$this->db->where("caidan_id", $caidan_id);
		$this->db->limit(intval($limit));
		$this->db->offset(intval($offset));
		return $this->db->get(self::DB_CAIPU);

        //$query = $this->db->query("SELECT q.cook_name,q.coverImageUrl,q.favorite,q.id FROM " . DB_TABLEPRE . "tid_qid as t," . DB_TABLEPRE . "caipu as q WHERE t.qid=q.id AND t.tid=$id LIMIT $start,$limit");
       // while ($caipu = $this->db->fetch_array($query)) {
        //    $caipulist[] = $caipu;
       // }
       // return $caipulist;
    }
    /**
     * [菜谱信息]
     * @param  [type] $id [菜谱id]
     */
    public function get_caidan_by_id($id)
    {
    	$data = array();
		$this->db->select('*')->from(self::DB_CAIDAN)->where('id', intval($id))->limit(1);
		$query = $this->db->get();
		if($query->num_rows() == 1)
		{
			$data = $query->row_array();
			$data['addtime'] = date("Y-m-d h:i", $data['addtime']);
			$data['uptime'] = date("Y-m-d h:i", $data['uptime']);
		}
		$query->free_result();
		
		return $data;
    }

    /**
     * [菜单列表]
     * @param  [type]  $offset   [偏移量]
     * @param  [type]  $limit    [显示数量]
     * @param  [type]  $authorid [所属用户]
     * @param  integer $status   [状态 0隐藏，1显示]
     * @param  string  $orderby  [排序]
     */
    public function get_caidan_list($offset = NULL, $limit = NULL, $uid = NULL, $status = NULL, $orderby = 'id DESC')
    {
		$this->db->select('*');
		
		if($status && is_numeric($status))
		{
			$this->db->where('status', intval($status));
		}
		else
		{
			//$this->db->where('status != 0');
		}

		if($uid && is_numeric($uid))
		{
			$this->db->where('uid', intval($uid));
		}

		if($limit && is_numeric($limit))
		{
			$this->db->limit(intval($limit));
		}
		
		if($offset && is_numeric($offset))
		{
			$this->db->offset(intval($offset));
		}
		$this->db->order_by($orderby); 
        return $this->db->get(self::DB_CAIDAN);
    }


    //搜索
    public function get_caidan_search($keywords, $offset = NULL, $limit = NULL, $orderby = 'id DESC')
    {
    	$keywords = explode('+',$keywords);
    	$keywords_num = count($keywords);
		$this->db->select('*');

		for($i=0;$i<$keywords_num;$i++)
		{
			$key = urldecode($keywords[$i]);
			$this->db->like('title', $this->db->escape_like_str($key));
		}
		
		$this->db->where('status', 1);

		if($limit && is_numeric($limit))
		{
			$this->db->limit(intval($limit));
		}
		
		if($offset && is_numeric($offset))
		{
			$this->db->offset(intval($offset));
		}

		$this->db->order_by($orderby); 

        return $this->db->get(self::DB_CAIDAN);
    }



    function update_views($id)
    {
		$this->db->set('views','views+1',FALSE);
		$this->db->where('id',$id);
    	return $this->db->update(self::DB_CAIDAN);
    }
	//***********************后台专用*********************************************************************
	public function caidan_insert($arr)
	{
		return $this->db->insert(self::DB_CAIDAN, $arr);
	}

	public function caidan_update($arr, $id)
	{
		return $this->db->update(self::DB_CAIDAN, $arr, 'id = '.$id);
	}

	public function caidan_delete($id)
	{
		return $this->db->where('id',$id)->delete(self::DB_CAIDAN);
	}
	public function delete_caidan_uid($uid)
	{
		return $this->db->where('uid',$uid)->delete(self::DB_CAIDAN);
	}
}

/* End of file caipu_model.php */
/* Location: ./application/models/caipu_model.php */